package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson2.JSONArray;
import com.nidu.demo.base.BatchDeleteCmd;
import com.nidu.demo.dict.api.SysDictTypeService;
import com.nidu.demo.dict.dto.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;

/**
 * 字典类型管理 Controller
 */
@Tag(name = "管理后台 - 字典类型")
@RestController
@RequestMapping("/system/dict-type")
@Validated
@RequiredArgsConstructor
public class SysDictTypeController {

    private final SysDictTypeService service;

    @Operation(summary = "创建字典类型", description = "创建新的字典类型")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:dict:create')")
    public SingleResponse<DictTypeCO> addDictType(@RequestBody DictTypeAddCmd cmd) {
        return service.add(cmd);
    }

    @Operation(summary = "更新字典类型", description = "更新字典类型的信息")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dict:update')")
    public SingleResponse<DictTypeCO> updateDictType(@PathVariable Long id, @RequestBody DictTypeUpdateCmd cmd) {
        cmd.setId(id);
        return service.update(cmd);
    }

    @Operation(summary = "删除字典类型", description = "根据 ID 删除字典类型")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    public Response deleteDictType(@PathVariable Long id) {
        return service.delete(id);
    }

    @Operation(summary = "批量删除字典类型", description = "根据 ID 列表删除多个字典类型")
    @PostMapping("/batch")
    @PreAuthorize("@ss.hasPermission('system:dict:delete')")
    public Response deleteCompanies(@RequestBody BatchDeleteCmd cmd) {
        List<Long> ids = JSONArray.parseArray(cmd.getDelete(), Long.class);
        return service.delete(new HashSet<>(ids));
    }

    @Operation(summary = "获取字典类型详情", description = "根据 ID 获取字典类型详细信息")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public SingleResponse<DictTypeCO> getDictTypeById(@PathVariable Long id) {
        return service.getById(id);
    }

    @Operation(summary = "查询字典类型列表", description = "获取所有字典类型信息")
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public MultiResponse<DictTypeCO> listCompanies(DictTypeListQry qry) {
        return service.list(qry);
    }

    @Operation(summary = "分页查询字典类型", description = "分页获取字典类型信息")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:dict:query')")
    public PageResponse<DictTypeCO> pageCompanies(DictTypePageQry qry) {
        return service.page(qry);
    }
}
